package dimensional.traveler.client.screens.cinematic;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.dom.ScrollSupport;
import com.sencha.gxt.core.client.dom.ScrollSupport.ScrollMode;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.FlowLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;

import dimensional.traveler.client.mvc.ControlledComposite;
import dimensional.traveler.client.mvc.MediatedController;
import dimensional.traveler.shared.Settings;
import dimensional.traveler.shared.model.ConversationLine;
import dimensional.traveler.shared.model.missionSequence.Cinematic;

public class CinematicScreen extends ControlledComposite implements SelectHandler {

	private static final int LINE_DISPLAY_INTERVAL = 3000;
	private static CinematicScreenUiBinder uiBinder = GWT.create(CinematicScreenUiBinder.class);

	interface CinematicScreenUiBinder extends UiBinder<Widget, CinematicScreen> {
	}

	@UiField
	Image backgroundImage;
	@UiField
	FlowLayoutContainer conversationContainer;
	@UiField
	TextButton skipButton;
	
	private CinematicController controller = new CinematicController();
	private List<ConversationLine> conversation = new ArrayList<ConversationLine>(0);
	
	public CinematicScreen(Cinematic cinematic) {
		initWidget(uiBinder.createAndBindUi(this));
		initCinematicBackground(cinematic.getBackgroundImageId());
		initSkipButton();
		setupConversation(cinematic.getConversation());
	}

	private void initCinematicBackground(String backgroundImageId) {
		if (!Settings.USE_PLACEHOLDERS)
			backgroundImage.setUrl("/images/cinematics/" + backgroundImageId + ".jpg");
	}
	
	private void initSkipButton() {
		skipButton.addSelectHandler(this);
	}
	
	private void setupConversation(List<ConversationLine> conversation) {
		this.conversation = new ArrayList<ConversationLine>(conversation.size());
		this.conversation.addAll(conversation);
	}
	
	@Override
	protected void onAttach() {
		super.onAttach();
		startConversation();
	}

	private void startConversation() {
		final Iterator<ConversationLine> currentLine = conversation.iterator();
		final ScrollSupport scrollSupport = conversationContainer.getScrollSupport();
		scrollSupport.setScrollMode(ScrollMode.AUTOY);
		Timer conversationTimer = setupTimer(currentLine, scrollSupport);
		conversationTimer.scheduleRepeating(LINE_DISPLAY_INTERVAL);
	}

	private Timer setupTimer(final Iterator<ConversationLine> currentLine, final ScrollSupport scrollSupport) {
		Timer conversationTimer = new Timer() {
			@Override
			public void run() {
				scrollSupport.scrollToBottom();
				if (!currentLine.hasNext()){
					this.cancel();
					skipButton.setText("Finish");
					return;
				}
				ConversationWidget conversationWidget = new ConversationWidget(currentLine.next());
				conversationContainer.add(conversationWidget); 
			}
		};
		return conversationTimer;
	}

	@Override
	public MediatedController getController() {
		return controller;
	}

	@Override
	public void onSelect(SelectEvent event) {
		endCinematic();
	}

	private void endCinematic() {
		controller.endCinematic();
	}

}
